//
//  PopUp.swift
//  YoruNeko
//
//  Created by Apple on 2021/6/21.
//

import SwiftUI

struct PopUp<Content:View>: View {
    var title: String
    
    var onCommit:((Bool)->Void)?
    
    var content:()->Content
    
    var body: some View {
        ZStack{
            Color.black.opacity(0.5).onTapGesture {
                onCommit?(false)
            }
            VStack {
                Text(title)
                    .padding()
                content()
                
                HStack {
                    Button(action: {
                        //取消键 Cancel
                        // 取消回调
                        onCommit?(false)
                    }, label: {
                        Text("Cancel")
                    })
                    
                    
                    Spacer()
                    
                    Button(action: {
                        //确认键 Done
                        // 确认回调
                        onCommit?(true)
                        
                    }, label: {
                        Text("Done")
                    })
                }
                .padding(.horizontal)
                .padding(.bottom)
            }
            .background(Color(.white))
            .clipShape(RoundedRectangle(cornerRadius: 10))
            .frame(width: 300, height: 200, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
            //以后根据屏幕大小调整
            .shadow(color: .gray, radius: 10, x: -5, y: 5)
            //            .offset(y: isEditing ? 0 : 900)

        }
    }
}

struct PopUp_Previews: PreviewProvider {
    @State static var text1: String="text1"
    @State static var text2: String="text2"
    static var previews: some View {
        PopUp(title: "编辑书签", content: {
            Group{
                Text("damn")
                TextField("siteName", text: $text1)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.horizontal)
                    .disableAutocorrection(true)
                    .autocapitalization(.none)

                TextField("url", text: $text2)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.horizontal)
                    .disableAutocorrection(true)
                    .autocapitalization(.none)
            }
        })
    }
}
